﻿using QD.SQLHelper;
using QD.Tools;
using QD_Clients.Models;
using System;
using System.Collections.Generic;
using System.Linq;

namespace QD_Clients.Code.DbContent
{
    public static class QDUserHelper
    {
        /// <summary>
        /// 当前登录用户
        /// </summary>
        public static QDUser CurUser;

        /// <summary>
        /// 取当前登录用户的角色集合
        /// </summary>
        /// <returns></returns>
        public static List<string> GetCurRoles()
        {
            List<string> roleList = CurUser.UType.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries).ToList();
            for (int i = 0; i < roleList.Count; i++)
            {
                roleList[i] = roleList[i].Trim();
            }
            return roleList;
        }


        /// <summary>
        /// 取当前登录用户主角色
        /// </summary>
        /// <returns></returns>
        public static LoginRole GetLoginMstRoleName()
        {
            if (CurUser == null)
            {
                return LoginRole.未登录;
            }
            else
            {
                if (!string.IsNullOrEmpty(CurUser.DltUType))
                {
                    return (LoginRole)Enum.Parse(typeof(LoginRole), CurUser.DltUType);
                }
                else
                {
                    List<string> roleList = GetCurRoles();

                    if (roleList.Count == 0)
                    {
                        return LoginRole.未定义;
                    }
                    else
                    {
                        return (LoginRole)Enum.Parse(typeof(LoginRole), roleList[0]);
                    }
                }
            }
        }

        /// <summary>
        /// 判断当前登录用户是否包含指定角色
        /// </summary>
        /// <param name="role"></param>
        /// <returns></returns>
        public static bool IsRole(LoginRole role)
        {
            List<string> roleList = GetCurRoles();
            string _role = role.ToString();
            bool isOk = roleList.Contains(_role);

            return isOk;
        }

        /// <summary>
        /// 消除登录状态
        /// </summary>
        public static void ClearLoginStatus()
        {
            DbConfig.Instance.LogonConfig.ServerName = "";
            DbConfig.Instance.LogonConfig.UserID = "";
            DbConfig.Instance.LogonConfig.PassWord = "";
            CurUser = null;
        }

        /// <summary>
        /// 登录用户
        /// </summary>
        /// <param name="_qdUid"></param>
        /// <param name="_qdPwd"></param>
        /// <param name="_cbSource"></param>
        /// <param name="_uid"></param>
        /// <param name="_pwd"></param>
        /// <param name="_logonType"></param>
        /// <returns></returns>
        public static bool LoginDb(string _qdUid, string _qdPwd, string _cbSource, string _uid, string _pwd, int _logonType)
        {
            DbConfig.Instance.DbDataBaseName = "QDDevPlat_V4";
            DbConfig.Instance.DbResourcesName = "QDResources";
            DbConfig.Instance.Uid = _qdUid;
            DbConfig.Instance.PWD = _qdPwd;
            DbConfig.Instance.LogonConfig.ServerName = _cbSource;
            DbConfig.Instance.LogonConfig.UserID = _uid;
            DbConfig.Instance.LogonConfig.PassWord = _pwd;
            DbConfig.Instance.LogonConfig.LogonType = _logonType;

            string errorMsg = string.Empty;
            MSDbContext db = new MSDbContext(DbConfig.Instance.GetDbDataBaseConnStr());


            if (!db.IsConnection(ref errorMsg))
            {
                QD.Tools.PromptHelper.Prompt("连接组织数据库失败！\r\n" + errorMsg);
                return false;
            }
            else
            {
                if (string.IsNullOrEmpty(_qdUid))
                {
                    QD.Tools.PromptHelper.Prompt("请输入平台登录用户名！");
                    return false;
                }
                else if (string.IsNullOrEmpty(_qdPwd))
                {
                    QD.Tools.PromptHelper.Prompt("请输入平台登录密码！");
                    return false;
                }
                else
                {
                    QDResources dbPlat = new QDResources();
                    QDUser user = dbPlat.QDUser.FirstOrDefault(p => p.Uid == _qdUid);
                    if (user == null || user.Pwd != _qdPwd)
                    {
                        PromptHelper.Prompt("用户名或密码错误！");
                        return false;
                    }
                    else if (user.IsDimission)
                    {
                        PromptHelper.Prompt("该员工已离职！");
                        return false;
                    }
                    else if (user.IsForbidLogon)
                    {
                        PromptHelper.Prompt("该账号禁止登录！");
                        return false;
                    }
                    else
                    {
                        CurUser = user;
                        return true;
                    }
                }
            }
        }
    }
}
